﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using theeg.DependencyAnalysis.CommonAddin.Models;

namespace DependencyVisualizer
{
    public class StandaloneSolution : ISolution, INotifyPropertyChanged
    {
        public Action BuildFinished { get; set; }

        public string FileName
        {
            get { return GetType().Assembly.Location; }
        }

        private IList<IProject> _projects = new List<IProject>();

        public IList<IProject> Projects
        {
            get { return _projects; }
        }

        public void Refresh()
        {
            var selectAssembliesPage = new SelectAssembliesPage();
            (from x in _projects orderby x.OutputFileName select x.OutputFileName).ToList()
                .ForEach(x => selectAssembliesPage.FileNames.Add(x));

            selectAssembliesPage.ShowDialog();
            var fileNames = selectAssembliesPage.FileNames;
            _projects = new List<IProject>();
            foreach (var file in fileNames)
            {
                var fileName = Path.GetFileName(file);
                _projects.Add(new StandaloneProject
                {
                    IsSelected = true,
                    Name = fileName,
                    OutputFileName = file
                });
            }
            RaisePropertyChanged("Projects");
        }

        public void Build()
        {
            var buildFinished = BuildFinished;
            if (buildFinished != null)
            {
                buildFinished();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}